"""
该文件定义了与机器人聊天相关的API接口。
功能1：自由聊天
功能2：情景聊天
"""

from fastapi import APIRouter, Depends, BackgroundTasks
from fastapi.security import OAuth2PasswordBearer
from sqlalchemy import select
from sqlalchemy.orm import Session

from models import User, engine
from schemas.chat_robot import ChatCompletionRequest

router = APIRouter()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


def verify_api_key(token: str = Depends(oauth2_scheme)) -> User:
    with Session(engine) as session:
        stmt = select(User).where(User.api_key == token)
        user = session.scalars(stmt).one()
    if not user:
        raise ValueError("Invalid API key")
    return user


@router.get('/v1/chat/completions')
async def chat_completions(
    request: ChatCompletionRequest,
    background_tasks: BackgroundTasks,
    user: User = Depends(verify_api_key),
):
    """
    处理聊天完成请求的API接口。
    """
    '''
    判断请求类型
    1. 自由聊天：直接返回聊天内容
    2. 创建情景：创建一个情景，开启新的聊天
    3. 更改情景：更改当前情景的设置
    4. 情景聊天：直接返回聊天内容
    5. 获取总结：获取当前情景的总结信息
    '''

    return {"message": "Chat completions endpoint is under construction."}
